package com.zkingsoft.services.distribution.impl;

import com.zkingsoft.services.distribution.DistributionImagesService;
import com.zkingsoft.pojo.PaginationVO;
import com.zkingsoft.model.distribution.DistributionImages;
import com.zkingsoft.dao.distribution.DistributionImagesDao;

import org.springframework.stereotype.Service;

import java.util.Map;

import com.zkingsoft.exception.GlobleException;
import com.zkingsoft.util.ModelUtils;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * This field was generated by Zking.software.Codegen.
 * @date 2017-06-16 15:22
 */
@Service("distributionImagesService")
public class  DistributionImagesServiceImpl implements    DistributionImagesService  {

	
	@Autowired
	private DistributionImagesDao distributionImagesDao;
	
	
	@Override
	public int add(DistributionImages distributionImages){
		
		return distributionImagesDao.insert(distributionImages);
		
	}
	
	@Override
	public int batchAdd(List<DistributionImages>  distributionImagesList) {
		int num = 0;

		int c = 10000;
		int size = distributionImagesList.size()/c + 1;
		for(int i=0; i<size; i++) {
			int begin = i*c;
			int end = (i+1)*c;
			end = end >= distributionImagesList.size() ? distributionImagesList.size() : end;
			List<DistributionImages> insertList = distributionImagesList.subList(begin, end);
			num += distributionImagesDao.batchInsert(insertList);
		}
		return num;
		
	}
	
	
   	
    @Override
	public int modifyByMap(DistributionImages oldDistributionImages
	,DistributionImages newDistributionImages){
	
		if (!ModelUtils.isModified(oldDistributionImages, newDistributionImages)) {
			return 1;
		}
		Map<String, Object> modifyMap=null;
		try {
			modifyMap = ModelUtils.comparePojo2Map(oldDistributionImages, newDistributionImages);
		} catch (Exception e) {
			throw new GlobleException("数据对比失败");
		}
		if (modifyMap.size() > 0) {
			modifyMap.put("imgId", oldDistributionImages.getImgId());
			distributionImagesDao.updateByMap(modifyMap);
		}
		return 1;
	}
	
	@Override
	public int modifyByModel(DistributionImages distributionImages){
	
		return distributionImagesDao.updateByModel(distributionImages);
	
	}
	
	
	
	@Override
	public int remove(List<Long> list){
	
		return distributionImagesDao.deleteByIds(list);
	
	}

	@Override
	public int removeById(Long imgId){
	
		return distributionImagesDao.deleteById(imgId);
	
	}
	
	@Override
	public int removeByModel(DistributionImages distributionImages){
	
		return distributionImagesDao.deleteByModel(distributionImages);
	
	}
	
	
	@Override
	public List<DistributionImages> findInPage(DistributionImages distributionImages,  PaginationVO pageVo){
	
		return distributionImagesDao.selectInPage(distributionImages , pageVo);
	
	}
	
	@Override
	public List<DistributionImages> findByModel(DistributionImages distributionImages){
	
		return distributionImagesDao.selectByModel(distributionImages);
	
	}
	
	@Override
	public int  findTotal(DistributionImages distributionImages){
	
		return distributionImagesDao.selectTotalRecord(distributionImages);
	
	}
	
	@Override
	public DistributionImages  findById(Long imgId){
	
		return distributionImagesDao.selectById(imgId);
	
	}

	@Override
	public Long addImg(DistributionImages distributionImages) {
		distributionImagesDao.insert(distributionImages);
		
		List<DistributionImages> list=distributionImagesDao.selectByModel(distributionImages);
		if(list.isEmpty()){
			return null;
		}
		Long imgId=list.get(0).getImgId();
		return imgId;
	}

	@Override
	public int modifyByList(DistributionImages distributionImages) {
		int i=0;
		List<DistributionImages> list=distributionImages.getImgList();
		for(DistributionImages dis:list){
			if(dis.getImgId()!=null && dis.getImgUrl()!=null){
				i+=distributionImagesDao.updateByModel(dis);
			}
		}
		return i;
	}

   	
	
	
}